package com.vessel.decoration.mvp.ui.fragment.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.vessel.decoration.R;
import com.vessel.decoration.app.base.MySupportFragment;
import com.vessel.decoration.app.utils.RecycleViewDivider;
import com.vessel.decoration.di.component.mine.DaggerCollectComponent;
import com.vessel.decoration.di.module.mine.CollectModule;
import com.vessel.decoration.mvp.contract.mine.CollectContract;
import com.vessel.decoration.mvp.presenter.mine.CollectPresenter;
import com.vessel.decoration.mvp.ui.adapter.CollectAdapter;
import com.vessel.decoration.mvp.ui.widget.CustomLoadMoreView;
import com.zhy.autolayout.AutoLinearLayout;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author vesselzhang
 * @date 2017/12/4
 */

public class MeCollectFragment extends MySupportFragment<CollectPresenter>
        implements CollectContract.View, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.tv_title)
    TextView mTitleTV;
    @BindView(R.id.tv_right)
    TextView mRightTV;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.btn_delete)
    Button mBtnDelete;
    @BindView(R.id.select_all)
    TextView mSelectAll;
    @BindView(R.id.me_collect_bottom)
    AutoLinearLayout mLlMycollectionBottomDialog;


    @Inject
    CollectAdapter mAdapter;

    public static MeCollectFragment newInstance() {
        return new MeCollectFragment();
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerCollectComponent
                .builder()
                .appComponent(appComponent)
                .collectModule(new CollectModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_me_collect, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitleTV.setText(ArmsUtils.getString(getActivity(), R.string.me_item_collect));
        mRightTV.setVisibility(View.VISIBLE);
        mRightTV.setText("编辑");
        initRecycleView();
        initAdapter();
        mPresenter.collectList(true);
    }

    @Override
    public void setData(Object data) {

    }

    private void initRecycleView() {
        mSwipeRefreshLayout.setOnRefreshListener(this);
        ArmsUtils.configRecycleView(mRecyclerView, new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL
                , 20, getResources().getColor(R.color.def_bg)));
    }

    private void initAdapter() {
        mAdapter.setEnableLoadMore(false);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
//        mAdapter.setOnLoadMoreListener(mPresenter, mRecyclerView);
//        mAdapter.setPreLoadNumber(5);
        mAdapter.setOnItemClickListener(mPresenter);
    }

    @OnClick({R.id.iv_left, R.id.tv_right, R.id.select_all, R.id.btn_delete})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_left:
                killMyself();
                break;
            case R.id.tv_right:
                mPresenter.updateEditMode();
                break;
            case R.id.select_all:
                mPresenter.selectAllMain();
                break;
            case R.id.btn_delete:
                mPresenter.collectOrCancel();
                break;
        }
    }

    @Override
    public void showLoading() {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(String message) {
        ArmsUtils.makeText(getActivity(), message);
    }

    @Override
    public void launchActivity(Intent intent) {
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        getActivity().finish();
    }

    @Override
    public void onRefresh() {
        mPresenter.collectList(true);
    }

    @Override
    public void updateModel(boolean isEditModel) {
        if (isEditModel) {
            mRightTV.setText("取消");
            mLlMycollectionBottomDialog.setVisibility(View.VISIBLE);
        } else {
            mRightTV.setText("编辑");
            mLlMycollectionBottomDialog.setVisibility(View.GONE);
        }
    }

    @Override
    public void updateBottomLayout(boolean deleteEnable, String selectStatus) {
        mBtnDelete.setEnabled(deleteEnable);
        mSelectAll.setText(selectStatus);
    }
}
